<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\Http\Requests\CreateTaskRequest;
use App\Task;
use Redirect, Input, Auth;
class TaskController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//echo 12325443645;
		//
		/*$model = new Task();
        $builder = $model->orderBy('id', 'desc');
		var_dump($builder);
		return view('task.task');*/
		$tasks = Task::paginate(1);
		$tasks->setPath('task');

		return view('task.task')->withTasks($tasks);
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
		return view('task.edit');
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store(CreateTaskRequest $request) {
		$task = new Task();
		$task->name = Input::get('name');
		$task->context = Input::get('context');
		$task->user_id = Auth::user()->id;
		
		//$task->attachment = Input::get('attachment');
		$file = Input::file('attachment');
		if($file -> isValid()){

			//检验一下上传的文件是否有效.

			$clientName = $file -> getClientOriginalName();
			$tmpName = $file ->getFileName(); // 缓存在tmp文件夹中的文件名 例如 php8933.tmp 这种类型的.

		    $realPath = $file -> getRealPath();    //这个表示的是缓存在tmp文件夹下的文件的绝对路径
			$entension = $file -> getClientOriginalExtension(); //上传文件的后缀.

			$mimeTye = $file -> getMimeType();//大家对mimeType应该不陌生了. 我得到的结果是 image/jpeg.
			$newName = md5(date('ymdhis'.$clientName)).$clientName;
			$path = $file -> move(app_path().'/storage/uploads',$newName);//如果我们希望将其放置在app的storage目录下的uploads目录中
		}
		$task->attachment = $path;
		$task->times = Input::get('times');
		$task->price = Input::get('price');
		$task->QQ = Input::get('QQ');
		$task->phone = Input::get('phone');
		
		$task->defend_time = Input::get('defend_time');
		$task->isBill = Input::get('isBill');
		$task->isContract = Input::get('isContract');
		$task->isTrademark = Input::get('isTrademark');
		$task->isCopy = Input::get('isCopy');
		
		$task->isSoftcopy = Input::get('isSoftcopy');
		$task->isDomain = Input::get('isDomain');
		$task->isApp = Input::get('isApp');
		$task->isUrgent = Input::get('isUrgent');
		$task->isSeach = Input::get('isSeach');
		
		$task->isTop = Input::get('isTop');
		$task->isHide = Input::get('isHide');
		$task->isDemand = Input::get('isDemand');
		$task->isHome = Input::get('isHome');

		if ($task->save()) {
			return Redirect::to('task');
		} else {
			return Redirect::back()->withInput()->withErrors('保存失败！');
		}
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
		//return view('task.edit');
		return view('task.show')->withOld(Task::find($id));
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
		echo 12324325;
		echo $id;
		//return view('task.edit');
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
